//
//  TopViewController.m
//  MtimeMovie
//
//  Created by lllyyy on 15/7/17.
//  Copyright (c) 2015年 lllyyy. All rights reserved.
//

#import "TopViewController.h"
#import "Header.h"
#import "DataService.h"
#import "MovieModel.h"
#import "TopMovieCell1.h"
@interface TopViewController ()
@end

@implementation TopViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self _createCollecttionView];
    [self loadData];
    self.view.backgroundColor = [UIColor redColor];
    [self.navigationItem setTitle:@"Top"];
}


- (void)_createCollecttionView{
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    
    layout.itemSize = CGSizeMake((KWidth-40)/3, (KWidth-40)/3*1.5);
  
    _collectionView = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:layout];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;

    [self.view addSubview:_collectionView];
    
    UINib *nib = [UINib nibWithNibName:@"TopMovieCell1" bundle:nil];
    [_collectionView registerNib:nib forCellWithReuseIdentifier:@"cell"];
}

- (void)loadData{
    _movieModalArray = [[NSMutableArray alloc]init];
    NSDictionary *jsonDic = [DataService getJsonDataFromFile:@"top250.json"];
    NSArray *subjects = [jsonDic objectForKey:@"subjects"];
    for (NSDictionary *dic in subjects) {
        MovieModel *modal = [[MovieModel alloc]init];
        [modal setValuesForKeysWithDictionary:dic];
        [_movieModalArray addObject:modal];
    }
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _movieModalArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    TopMovieCell1 *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.modal = _movieModalArray[indexPath.row];
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
